package Start;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.awt.*;
public class Carculator extends JFrame implements ActionListener {
    private  JPanel panel = new JPanel();//面板
    private  JTextField textField = new JTextField();//文本框
    private  JButton buttons = new JButton("clear");//按钮
    //private  String[] buttonText = {"7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/", "C", "(", ")"};
    private  JPanel panelcenter = new JPanel();//面板
    public Carculator(){
        //setIconImage(Toolkit.getDefaultToolkit().getImage("image/login/background.png"));


        this.init();
        this.AddComponent();
        this.AddComponent2();


    }
    public void init(){
        this.setTitle("计算器");
        this.setSize(500, 500);
        this.setLayout(new BorderLayout());
        this.setResizable(false);//设置不可以改变大小
        this.setLocationRelativeTo(null);//设置居中显示
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }

    public void AddComponent (){

        this.textField.setPreferredSize(new Dimension(230, 30));
        panel.add(textField);
        this.buttons.setForeground(Color.RED);

        panel.add(buttons);
        buttons.addActionListener(this);

        // 将面板添加到 JFrame 中
        this.add(panel, BorderLayout.NORTH);

    }

    public void AddComponent2(){
        String text = "123+456-789*0.=/";
        String regex = "[+*/.=-]";


        this.panelcenter.setLayout(new GridLayout(4, 4));
       // this.panelcenter.setPreferredSize(new Dimension(230, 230));
        for (int i = 0; i < 16; i++) {
            String s = text.substring(i,i+1);
            JButton bu = new JButton();
            bu.setText(s);
            if(s.matches(regex)){
                bu.setFont(new Font("宋体", Font.BOLD, 20));
                bu.setForeground(Color.RED);
            }
            bu.addActionListener(this);
            panelcenter.add(bu);
        }
        this.add(panelcenter,BorderLayout.CENTER);
    }

    public static void main(String[] args) {
        Carculator ca = new Carculator();
        ca.setVisible(true);
    }
    private String fistnum  = null;
    private  String oper = null;
    private  String secondnum = null;

    @Override
    public void actionPerformed(ActionEvent e) {
        String ac = e.getActionCommand();
        if("0123456789.".indexOf(ac) != -1){
            this.textField.setText(textField.getText()+ac);//获取文本框中的文本
            this.textField.setHorizontalAlignment(JTextField.RIGHT);//设置文本框中的文本居右显示
            //JOptionPane.showMessageDialog(this, ac);
        } else if (ac.matches("[+\\-*/]")) {
            oper = ac;
            fistnum = textField.getText();
            this.textField.setText("");


        } else if (ac.equals("=")) {
            Double num1 = Double.valueOf(fistnum);
            Double num2 = Double.valueOf(this.textField.getText());

            Double result = null;
            switch (oper){
                case  "+"->  result = num2+num1;
                case "-"-> result = num1-num2;
                case "*"-> result = num1*num2;
                case "/"->{if(num2 !=0 ){result = num1/num2;}
                    }

            }
            this.textField.setText(result.toString());
        }else if(ac.equals("clear")){
            this.textField.setText("");
        }


    }
}
